<%@page import="model.type.SubjectAttributeType"%>
<%@page import="model.Enterprise"%>
<%@page import="model.Person"%>
<%@page import="search.SearchString"%>
<%@page import="search.SearchAttribute"%>
<%@page import="web.Html"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<% 
	ArrayList<SubjectAttributeType> customer = (ArrayList<SubjectAttributeType>)request.getAttribute("customer_attributes");
	ArrayList<SubjectAttributeType> enterprise = (ArrayList<SubjectAttributeType>)request.getAttribute("enterprise_attributes");
	ArrayList<SubjectAttributeType> employee = (ArrayList<SubjectAttributeType>)request.getAttribute("employee_attributes");
	ArrayList<SubjectAttributeType> person = (ArrayList<SubjectAttributeType>)request.getAttribute("person_attributes");
	
	ArrayList<Enterprise> resultEnterprise = (ArrayList<Enterprise>)request.getAttribute("result_enterprise");
	ArrayList<Person> resultPerson = (ArrayList<Person>)request.getAttribute("result_person");
	
	SearchString searchString = (SearchString)request.getAttribute("search_string");
	if(searchString == null) searchString = new SearchString();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link type="text/css" rel="stylesheet" href="./css/style.css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>

</head>
<body>
	<script type="text/javascript">   
		$(document).ready(function(){
			<% if(searchString.getType() != null)out.print("$('#type option').eq("+(searchString.getType()-1)+").attr('selected', 'selected');"); %>
			
			<% 			
			for(SearchAttribute s: searchString.getAttributes()){
				out.println("$('#"+s.getType()+"').val('"+s.getValue()+"');");
			}			
			%>
			
	   		$('#type').change(function() {
	   			var str = 0;
	            $("select option:selected").each(function () {
	               str = $(this).val();
	            });	            
	   			if(str != "1")$(".atr1").hide();
	   			else $(".atr1").show();
	   			if(str != "2")$(".atr2").hide(); 
	   			else $(".atr2").show();
	   			if(str != "3")$(".atr3").hide(); 
	   			else $(".atr3").show();
	   			if(str != "4")$(".atr4").hide();
	   			else $(".atr4").show();	           		
	    	}).change();   		
	   	});
		
	</script> 		
	<a href=".">Back</a><br/>
	<div class="searchdiv">
	<form name ="search_form" id="search_form" action="./search" method="post">
		<table>			
			<tr><td>Name</td><td><input type="text" name="name" value="<% out.print(Html.notNull(searchString.getName())); %>"></input></td></tr>
			<tr><td>Subject type</td><td>
				<select  id="type" name ="type">
					<option value="1">Person</option>
					<option value="2">Enterprise</option>
					<option value="3">Employee</option>
					<option value="4">Customer</option>
				</select>
			</td></tr>
			<% out.print(Html.searchAttributes(person, "atr1")); %>
			<% out.print(Html.searchAttributes(enterprise, "atr2")); %>
			<% out.print(Html.searchAttributes(employee, "atr3")); %>
			<% out.print(Html.searchAttributes(customer, "atr4")); %>
			<tr><td><hr /></td><td><hr /></td></tr>
			<tr><td>Country</td><td><input type="text" name="country" value="<% out.print(Html.notNull(searchString.getCountry())); %>"/></td></tr>
			<tr><td>County</td><td><input type="text" name="county" value="<% out.print(Html.notNull(searchString.getCounty())); %>"/></td></tr>
			<tr><td>City</td><td><input type="text" name="city" value="<% out.print(Html.notNull(searchString.getCity())); %>"/></td></tr>
			<tr><td>Address</td><td><input type="text" name="address" value="<% out.print(Html.notNull(searchString.getAddress())); %>"/></td></tr>
			<tr><td>Index</td><td><input type="text" name="index" value="<% out.print(Html.notNull(searchString.getZip())); %>"/></td></tr>
			<tr><td><hr /></div></td><td><hr /></td></tr>
			<tr><td>Contact</td><td><input type="text" name="contact" value="<% out.print(Html.notNull(searchString.getContact())); %>"/></td></tr>
			<tr><td><input type="submit" value="search" id="search_button"></td><td></td></tr>
		</table>		
	</form>
	</div>
	<div id="results" class="searchdiv">
		<table>		
		<% 
			
			if(resultPerson != null){
				if(resultPerson.size() > 0){ 
					out.println("<tr><td>Found "+resultPerson.size()+" persons: </td></tr>");
				} else out.println("<tr><td>No matches in persons.</td></tr>");
			
				for(Person p : resultPerson){
					out.println("<tr><td>"+Html.personLink(p)+"</td></tr>");
				}
			}
			
			if(resultEnterprise != null){
				if(resultEnterprise.size() > 0){ 
					out.println("<tr><td>Found "+resultEnterprise.size()+" enterprises: </td></tr>");
				} else out.println("<tr><td>No matches in enterprises.</td></tr>");
				
				for(Enterprise e : resultEnterprise){
					out.println("<tr><td>"+Html.enterpriseLink(e)+"</td></tr>");
				}
			}
		%>
		</table>
	</div>
</body>
</html>